import Vue from "vue";
import VueRouter from "vue-router";
import { getsessionStorage } from '../sessionStorage/index'
const Login = () => import('../views/Login.vue')
const Home = () => import('../views/Home.vue')
const AdminIndex = () => import('../views/AdminIndex.vue');
const Adminlogin = () => import('../views/Adminlogin.vue');
const LoginRegister = () => import('../components/Login/Loginregister.vue');
const HomeIndex = () => import('../components/Home/HomeIndex.vue');
const LoginLogin = () => import('../components/Login/LoginLogin.vue');
const HomeAdd = () => import('../components/Home/HomeAdd.vue');
const Details = () => import('../components/Home/Details.vue');
const News = () => import('../components/Admin/News.vue');
const User = () => import('../components/Admin/User.vue');
const Type = () => import('../components/Admin/Type.vue');
const Swiper = () => import('../components/Admin/Swiper.vue');
const Sensitives = () => import('../components/Admin/Sensitives.vue');
const Admin = () => import('../components/Admin/Admin.vue');
const Charts = () => import('../components/Admin/Charts.vue');
const UserType = () => import('../components/Home/Type.vue');

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: '/login'
  },
  {
    path: "/login",
    component: Login,
    children: [
      {
        path: '',
        redirect: 'login'
      },
      {
        path: 'login',
        name: 'LoginLogin',
        component: LoginLogin
      },
      {
        path: 'register',
        name: 'Loginregister',
        component: LoginRegister
      }
    ]
  },
  {
    path: '/home',
    component: Home,
    meta: { auth: true },
    children: [
      {
        path: '',
        redirect: 'index'
      },
      {
        path: 'index',
        name: 'index',
        component: HomeIndex
      },
      {
        path: 'add',
        name: 'add',
        component: HomeAdd
      }, {
        path: 'info',
        name: 'info',
        component: Details
      }, {
        path: 'type',
        name: 'type',
        component: UserType
      }
    ]
  },
  {
    path: '/Adminlogin',
    name: '登录',
    component: Adminlogin,
  },
  {
    path: '/admin',
    name: '首页',
    component: AdminIndex,
    children: [{
      path: 'news',
      name: 'news',
      component: News
    }, {
      path: 'user',
      name: 'user',
      component: User
    }, {
      path: 'type',
      name: 'type',
      component: Type
    },
    {
      path: 'swiper',
      name: 'swiper',
      component: Swiper
    },
    {
      path: 'sensitives',
      name: 'sensitives',
      component: Sensitives
    },
    {
      path: 'admin',
      name: 'admin',
      component: Admin
    },
    {
      path: 'charts',
      name: 'charts',
      component: Charts
    }
    ]
  }
];

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  //判断是否需要登录
  if (to.matched.some(m => m.meta.auth)) {
    if (getsessionStorage('userInfo') != undefined) {

    } else if (to.path !== '/') {
      next({ path: '/login' })
      Vue.prototype.$message({
        type: 'error',
        message: '请先登录'
      })
    }
  }
  //判断是否访问的界面是否存在
  if (to.matched.length == 0) {
    from.name ? next({
      name: from.name
    }) : next('/login');
  } else {
    next();
  }
})

export default router;
